<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>使用HttpClient和OkHttp实现模拟登录方正教务系统 - MC&#39; blog</title><meta name="Description" content="MC&#39; Blog，不定期的技术分享"><meta property="og:title" content="使用HttpClient和OkHttp实现模拟登录方正教务系统" />
<meta property="og:description" content="使用HttpClient和OkHttp实现模拟登录方正教务系统 因为后面的项目需要，所以研究了一下方正教务系统的模拟登录。 首先分析一下，如何模" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/" /><meta property="og:image" content="http://example.org/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2018-07-16T10:05:00+08:00" />
<meta property="article:modified_time" content="2018-07-16T10:05:00+08:00" /><meta property="og:site_name" content="MC&#39; Blog" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="http://example.org/logo.png"/>

<meta name="twitter:title" content="使用HttpClient和OkHttp实现模拟登录方正教务系统"/>
<meta name="twitter:description" content="使用HttpClient和OkHttp实现模拟登录方正教务系统 因为后面的项目需要，所以研究了一下方正教务系统的模拟登录。 首先分析一下，如何模"/>
<meta name="application-name" content="MC&#39; Blog">
<meta name="apple-mobile-web-app-title" content="MC&#39; Blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="http://example.org/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/" /><link rel="prev" href="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8B/" /><link rel="next" href="http://example.org/threadlocal/" /><link rel="stylesheet" href="/css/style.min.css"><link rel="preload" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css"></noscript><link rel="preload" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css"></noscript><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "使用HttpClient和OkHttp实现模拟登录方正教务系统",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "http:\/\/example.org\/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F\/"
        },"image": ["http:\/\/example.org\/images\/Apple-Devices-Preview.png"],"genre": "posts","keywords": "爬虫","wordcount":  1718 ,
        "url": "http:\/\/example.org\/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F\/","datePublished": "2018-07-16T10:05:00+08:00","dateModified": "2018-07-16T10:05:00+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "http:\/\/example.org\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "MC"
            },"description": ""
    }
    </script></head>
    <body data-header-desktop="fixed" data-header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="MC&#39; blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw' aria-hidden='true'></i></span>MC&#39; Blog</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/categories/documentation/"> 文档 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/woxin123" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i>  </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
                </a></div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="MC&#39; blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw' aria-hidden='true'></i></span>MC&#39; Blog</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/categories/documentation/" title="">文档</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/woxin123" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
            </a></div>
    </div>
</header><div class="search-dropdown desktop">
        <div id="search-dropdown-desktop"></div>
    </div>
    <div class="search-dropdown mobile">
        <div id="search-dropdown-mobile"></div>
    </div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animate__animated animate__flipInX">使用HttpClient和OkHttp实现模拟登录方正教务系统</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel="author" class="author"><i class="fas fa-user-circle fa-fw" aria-hidden="true"></i>MC</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/%E5%85%B6%E4%BB%96/"><i class="far fa-folder fa-fw" aria-hidden="true"></i>其他</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw" aria-hidden="true"></i>&nbsp;<time datetime="2018-07-16">2018-07-16</time>&nbsp;<i class="fas fa-pencil-alt fa-fw" aria-hidden="true"></i>&nbsp;约 1718 字&nbsp;
                <i class="far fa-clock fa-fw" aria-hidden="true"></i>&nbsp;预计阅读 4 分钟&nbsp;<span id="/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/" class="leancloud_visitors" data-flag-title="使用HttpClient和OkHttp实现模拟登录方正教务系统">
                        <i class="far fa-eye fa-fw" aria-hidden="true"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
                    </span>&nbsp;</div>
        </div><div class="details toc" id="toc-static"  data-kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right" aria-hidden="true"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#使用httpclient和okhttp实现模拟登录方正教务系统">使用HttpClient和OkHttp实现模拟登录方正教务系统</a>
      <ul>
        <li><a href="#httpclient模拟登录">HttpClient模拟登录</a></li>
        <li><a href="#okhttp模拟登录">OkHttp模拟登录</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h2 id="使用httpclient和okhttp实现模拟登录方正教务系统">使用HttpClient和OkHttp实现模拟登录方正教务系统</h2>
<p>因为后面的项目需要，所以研究了一下方正教务系统的模拟登录。
首先分析一下，如何模拟登录需要什么参数。
下面的这个是验证码的url:
<img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdn.net/20180716010320956?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70"
        data-srcset="https://img-blog.csdn.net/20180716010320956?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70, https://img-blog.csdn.net/20180716010320956?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 1.5x, https://img-blog.csdn.net/20180716010320956?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 2x"
        data-sizes="auto"
        alt="https://img-blog.csdn.net/20180716010320956?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70"
        title="这里写图片描述" /></p>
<p>下面的这个是模拟登录需要的参数：
<img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdn.net/20180716010337636?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70"
        data-srcset="https://img-blog.csdn.net/20180716010337636?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70, https://img-blog.csdn.net/20180716010337636?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 1.5x, https://img-blog.csdn.net/20180716010337636?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 2x"
        data-sizes="auto"
        alt="https://img-blog.csdn.net/20180716010337636?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70"
        title="这里写图片描述" /></p>
<h3 id="httpclient模拟登录">HttpClient模拟登录</h3>
<p>HttpClient是Apahe下的一个项目，目的是为了方便Java发送请求，因为JDK提供的工具实在是他简陋了。
我这里是使用的maven构建的项目，所以在项目中添加下面的这个依赖即可：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="line"><span class="cl"> <span class="nt">&lt;dependency&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;groupId&gt;</span>org.apache.httpcomponents<span class="nt">&lt;/groupId&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;artifactId&gt;</span>httpclient<span class="nt">&lt;/artifactId&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;version&gt;</span>4.5.6<span class="nt">&lt;/version&gt;</span>
</span></span><span class="line"><span class="cl"><span class="nt">&lt;/dependency&gt;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>使用HttpClient的时候，第一步是创建一个HttpClient，它就相当于一个浏览器一样，所有的请求都是通过它发送出去的。
发送Get请求的时候，创建一个HttpGet即可，然后使用HttpClient发送出去。Post请求类似。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">public class HttpClientDemoTest5 {
</span></span><span class="line"><span class="cl">    public static final String BASEURL = &#34;http://xxx.xxx.xxx/&#34;;
</span></span><span class="line"><span class="cl">    public static final String LOGIN = &#34;default2.aspx&#34;;
</span></span><span class="line"><span class="cl">    public static final String HIDDEN_NAME = &#34;__VIEWSTATE&#34;;
</span></span><span class="line"><span class="cl">    public static final String HIDDEN_VALUE = &#34;dDwxNTMxMDk5Mzc0Ozs+lYSKnsl/mKGQ7CKkWFJpv0btUa8=&#34;;
</span></span><span class="line"><span class="cl">    public static final String LOGIN_USERNAME = &#34;txtUserName&#34;;
</span></span><span class="line"><span class="cl">    public static final String LOGIN_PASSWORD = &#34;TextBox2&#34;;
</span></span><span class="line"><span class="cl">    public static final String LOGIN_VALIDE_CODE = &#34;txtSecretCode&#34;;
</span></span><span class="line"><span class="cl">    public static final String URL_VALIDE_CODE = &#34;CheckCode.aspx&#34;;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    public static void main(String[] args) throws IOException {
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        // 创建一个HttpClient
</span></span><span class="line"><span class="cl">        HttpClient httpClient = HttpClients.createDefault();
</span></span><span class="line"><span class="cl">        // 首先发送一个请求，和该网站建立连接
</span></span><span class="line"><span class="cl">        HttpGet httpGet = new HttpGet(BASEURL);
</span></span><span class="line"><span class="cl">        // 发送请求，得到响应HttpResponse
</span></span><span class="line"><span class="cl">        HttpResponse first = httpClient.execute(httpGet);
</span></span><span class="line"><span class="cl">        // 获取headers中的Cookie，因为Cookie中有sessionID
</span></span><span class="line"><span class="cl">        Header[] headers = first.getHeaders(&#34;Set-Cookie&#34;);
</span></span><span class="line"><span class="cl">        String sessionId = headers[0].getValue();
</span></span><span class="line"><span class="cl">        // 重新建立一个header
</span></span><span class="line"><span class="cl">        Header session = new BasicHeader(&#34;Cookie&#34;, sessionId);
</span></span><span class="line"><span class="cl">        // 获取验证码的请求
</span></span><span class="line"><span class="cl">        HttpGet validateCode = new HttpGet(BASEURL + &#34;/&#34; + URL_VALIDE_CODE);
</span></span><span class="line"><span class="cl">        // 它的响应是一张图片，将图片写入文件保存
</span></span><span class="line"><span class="cl">        HttpResponse response = httpClient.execute(validateCode);
</span></span><span class="line"><span class="cl">        HttpEntity entity = response.getEntity();
</span></span><span class="line"><span class="cl">        if (entity != null) {
</span></span><span class="line"><span class="cl">            File file = new File(&#34;validate.png&#34;);
</span></span><span class="line"><span class="cl">            if (file.exists())
</span></span><span class="line"><span class="cl">                file.delete();
</span></span><span class="line"><span class="cl">            InputStream inputStream = entity.getContent();
</span></span><span class="line"><span class="cl">            Files.copy(inputStream, file.toPath());
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        // 发送POST请求，模拟登录
</span></span><span class="line"><span class="cl">        HttpPost httpPost = new HttpPost(&#34;http://222.24.62.120/&#34; + LOGIN);
</span></span><span class="line"><span class="cl">        List&lt;NameValuePair&gt; params = new ArrayList&lt;&gt;();
</span></span><span class="line"><span class="cl">        params.add(new BasicNameValuePair(HIDDEN_NAME, HIDDEN_VALUE));
</span></span><span class="line"><span class="cl">        params.add(new BasicNameValuePair(LOGIN_USERNAME, &#34;xxxxxxx&#34;));
</span></span><span class="line"><span class="cl">        params.add(new BasicNameValuePair(&#34;TextBox1&#34;, &#34;xxxxxxx&#34;));
</span></span><span class="line"><span class="cl">        params.add(new BasicNameValuePair(LOGIN_PASSWORD, &#34;xxxxxxxxx&#34;));
</span></span><span class="line"><span class="cl">        params.add(new BasicNameValuePair(&#34;RadioButtonList1&#34;, &#34;学生&#34;));
</span></span><span class="line"><span class="cl">        params.add(new BasicNameValuePair(&#34;Button1&#34;, &#34;&#34;));
</span></span><span class="line"><span class="cl">        Scanner scanner = new Scanner(System.in);
</span></span><span class="line"><span class="cl">        // 输入验证码
</span></span><span class="line"><span class="cl">        String code = scanner.nextLine();
</span></span><span class="line"><span class="cl">        params.add(new BasicNameValuePair(LOGIN_VALIDE_CODE, code));
</span></span><span class="line"><span class="cl">        httpPost.setEntity(new UrlEncodedFormEntity(params));
</span></span><span class="line"><span class="cl">        // 如果状态是302，那么表示登录成功。这里是有一个跳转，但是HttpClient并不会跳转
</span></span><span class="line"><span class="cl">        if (response.getStatusLine().getStatusCode() == 302) {
</span></span><span class="line"><span class="cl">            System.out.println(&#34;登录成功&#34;);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        // 获取跳转的url
</span></span><span class="line"><span class="cl">        Header headerLocation = response.getFirstHeader(&#34;Location&#34;);
</span></span><span class="line"><span class="cl">        String location =  headerLocation.getValue();
</span></span><span class="line"><span class="cl">        System.out.println(location);
</span></span><span class="line"><span class="cl">        HttpGet mypage = new HttpGet(BASEURL + location);
</span></span><span class="line"><span class="cl">        // 这个需要添加上面保存的session，是因为下面的httpClient发送请求的时候会一直发送，无法产生结果
</span></span><span class="line"><span class="cl">        // 这个具体不知道是为什么，所以在这里重新创建了一个httpClient，如下
</span></span><span class="line"><span class="cl">        mypage.setHeader(session);
</span></span><span class="line"><span class="cl">//        HttpResponse mypageResponse = httpClient.execute(mypage);
</span></span><span class="line"><span class="cl">        HttpResponse mypageResponse = HttpClients.createDefault().execute(mypage);
</span></span><span class="line"><span class="cl">        HttpEntity mypageEntiry = mypageResponse.getEntity();
</span></span><span class="line"><span class="cl">        if (mypageEntiry != null) {
</span></span><span class="line"><span class="cl">            InputStream content = mypageEntiry.getContent();
</span></span><span class="line"><span class="cl">            byte[] bytes = new byte[1024];
</span></span><span class="line"><span class="cl">            while (content.read(bytes) != -1)
</span></span><span class="line"><span class="cl">                System.out.println(new String(bytes, &#34;GBK&#34;));
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="okhttp模拟登录">OkHttp模拟登录</h3>
<p>在项目中添加OkHttp的依赖：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="line"><span class="cl"> <span class="nt">&lt;dependency&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;groupId&gt;</span>com.squareup.okhttp3<span class="nt">&lt;/groupId&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;artifactId&gt;</span>okhttp<span class="nt">&lt;/artifactId&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;version&gt;</span>3.11.0<span class="nt">&lt;/version&gt;</span>
</span></span><span class="line"><span class="cl"><span class="nt">&lt;/dependency&gt;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>上面的HttpClient模拟登录时，不知道为什么，最后的httpClient的请求总是得不到结果，一直等待，所以就有研究了下如何使用OkHttp模拟登录，不得不说的是OkHttp的模拟登录要比HttpClient简单。
流程和上面差不多，首先创建一个OkHttpClient。
这里重写了CookJar是的OkHttpClient能够自动管理Cookie。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="c1">// 设置OKHttpClient自动管理Cookie
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kd">private</span> <span class="kd">static</span> <span class="n">OkHttpClient</span> <span class="n">mOkHttpClient</span> <span class="o">=</span> <span class="k">new</span> <span class="n">OkHttpClient</span><span class="o">.</span><span class="na">Builder</span><span class="o">().</span><span class="na">cookieJar</span><span class="o">(</span><span class="k">new</span> <span class="n">CookieJar</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="kd">private</span> <span class="kd">final</span> <span class="n">HashMap</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;&gt;</span> <span class="n">cookieStore</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">        <span class="kd">public</span> <span class="kt">void</span> <span class="nf">saveFromResponse</span><span class="o">(</span><span class="n">HttpUrl</span> <span class="n">url</span><span class="o">,</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;</span> <span class="n">cookies</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">cookieStore</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">url</span><span class="o">.</span><span class="na">host</span><span class="o">(),</span> <span class="n">cookies</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">        <span class="kd">public</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;</span> <span class="nf">loadForRequest</span><span class="o">(</span><span class="n">HttpUrl</span> <span class="n">url</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">List</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;</span> <span class="n">cookies</span> <span class="o">=</span> <span class="n">cookieStore</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">url</span><span class="o">.</span><span class="na">host</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="n">cookies</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">?</span> <span class="n">cookies</span> <span class="o">:</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;();</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}).</span><span class="na">build</span><span class="o">();</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>下面的基本上和httpClient相似。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">OkHttpDemoTest</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 设置OKHttpClient自动管理Cookie
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kd">private</span> <span class="kd">static</span> <span class="n">OkHttpClient</span> <span class="n">mOkHttpClient</span> <span class="o">=</span> <span class="k">new</span> <span class="n">OkHttpClient</span><span class="o">.</span><span class="na">Builder</span><span class="o">().</span><span class="na">cookieJar</span><span class="o">(</span><span class="k">new</span> <span class="n">CookieJar</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="kd">private</span> <span class="kd">final</span> <span class="n">HashMap</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;&gt;</span> <span class="n">cookieStore</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">        <span class="kd">public</span> <span class="kt">void</span> <span class="nf">saveFromResponse</span><span class="o">(</span><span class="n">HttpUrl</span> <span class="n">url</span><span class="o">,</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;</span> <span class="n">cookies</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">cookieStore</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">url</span><span class="o">.</span><span class="na">host</span><span class="o">(),</span> <span class="n">cookies</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">        <span class="kd">public</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;</span> <span class="nf">loadForRequest</span><span class="o">(</span><span class="n">HttpUrl</span> <span class="n">url</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">List</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;</span> <span class="n">cookies</span> <span class="o">=</span> <span class="n">cookieStore</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">url</span><span class="o">.</span><span class="na">host</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="n">cookies</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">?</span> <span class="n">cookies</span> <span class="o">:</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;();</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}).</span><span class="na">build</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">BASEURL</span> <span class="o">=</span> <span class="s">&#34;http://xxx.xxx.xxx.xxx/&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">LOGIN</span> <span class="o">=</span> <span class="s">&#34;default2.aspx&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">HIDDEN_NAME</span> <span class="o">=</span> <span class="s">&#34;__VIEWSTATE&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">HIDDEN_VALUE</span> <span class="o">=</span> <span class="s">&#34;dDwxNTMxMDk5Mzc0Ozs+lYSKnsl/mKGQ7CKkWFJpv0btUa8=&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">LOGIN_USERNAME</span> <span class="o">=</span> <span class="s">&#34;txtUserName&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">LOGIN_PASSWORD</span> <span class="o">=</span> <span class="s">&#34;TextBox2&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">LOGIN_VALIDE_CODE</span> <span class="o">=</span> <span class="s">&#34;txtSecretCode&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">URL_VALIDE_CODE</span> <span class="o">=</span> <span class="s">&#34;CheckCode.aspx&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="n">Request</span> <span class="n">request</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Request</span><span class="o">.</span><span class="na">Builder</span><span class="o">().</span><span class="na">url</span><span class="o">(</span><span class="n">BASEURL</span><span class="o">).</span><span class="na">build</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">Call</span> <span class="n">call</span> <span class="o">=</span> <span class="n">mOkHttpClient</span><span class="o">.</span><span class="na">newCall</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">try</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">Response</span> <span class="n">response</span> <span class="o">=</span> <span class="n">call</span><span class="o">.</span><span class="na">execute</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="n">Request</span> <span class="n">validateRequest</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Request</span><span class="o">.</span><span class="na">Builder</span><span class="o">()</span>
</span></span><span class="line"><span class="cl">                <span class="o">.</span><span class="na">url</span><span class="o">(</span><span class="n">BASEURL</span> <span class="o">+</span> <span class="n">URL_VALIDE_CODE</span><span class="o">).</span><span class="na">build</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">Call</span> <span class="n">validateCall</span> <span class="o">=</span> <span class="n">mOkHttpClient</span><span class="o">.</span><span class="na">newCall</span><span class="o">(</span><span class="n">validateRequest</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">try</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">Response</span> <span class="n">validateResponse</span> <span class="o">=</span> <span class="n">validateCall</span><span class="o">.</span><span class="na">execute</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">            <span class="n">File</span> <span class="n">file</span> <span class="o">=</span> <span class="k">new</span> <span class="n">File</span><span class="o">(</span><span class="s">&#34;validateCode.png&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="o">(</span><span class="n">file</span><span class="o">.</span><span class="na">exists</span><span class="o">())</span>
</span></span><span class="line"><span class="cl">                <span class="n">file</span><span class="o">.</span><span class="na">delete</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">            <span class="n">InputStream</span> <span class="n">inputStream</span> <span class="o">=</span> <span class="n">validateResponse</span><span class="o">.</span><span class="na">body</span><span class="o">().</span><span class="na">byteStream</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">            <span class="n">Files</span><span class="o">.</span><span class="na">copy</span><span class="o">(</span><span class="n">inputStream</span><span class="o">,</span> <span class="n">file</span><span class="o">.</span><span class="na">toPath</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">        <span class="n">FormBody</span><span class="o">.</span><span class="na">Builder</span> <span class="n">formBodyBuilder</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FormBody</span><span class="o">.</span><span class="na">Builder</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">formBodyBuilder</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">HIDDEN_NAME</span><span class="o">,</span> <span class="n">HIDDEN_VALUE</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">formBodyBuilder</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">LOGIN_USERNAME</span><span class="o">,</span> <span class="s">&#34;xxxxxx&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">formBodyBuilder</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">&#34;TextBox1&#34;</span><span class="o">,</span> <span class="s">&#34;xxxxxxx&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">formBodyBuilder</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">LOGIN_PASSWORD</span><span class="o">,</span> <span class="s">&#34;xxxxxxxxx&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">formBodyBuilder</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">&#34;RadioButtonList1&#34;</span><span class="o">,</span> <span class="s">&#34;学生&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">formBodyBuilder</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">&#34;Button1&#34;</span><span class="o">,</span> <span class="s">&#34;&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">Scanner</span> <span class="n">scanner</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Scanner</span><span class="o">(</span><span class="n">System</span><span class="o">.</span><span class="na">in</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">String</span> <span class="n">code</span> <span class="o">=</span> <span class="n">scanner</span><span class="o">.</span><span class="na">nextLine</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">formBodyBuilder</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">LOGIN_VALIDE_CODE</span><span class="o">,</span> <span class="n">code</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">RequestBody</span> <span class="n">requestBody</span> <span class="o">=</span> <span class="n">formBodyBuilder</span><span class="o">.</span><span class="na">build</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">Request</span> <span class="n">loginRequest</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Request</span><span class="o">.</span><span class="na">Builder</span><span class="o">().</span><span class="na">url</span><span class="o">(</span><span class="n">BASEURL</span> <span class="o">+</span> <span class="n">LOGIN</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">                <span class="o">.</span><span class="na">post</span><span class="o">(</span><span class="n">requestBody</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">                <span class="o">.</span><span class="na">build</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// 不同于httpClient的是Okhttp会自动跳转
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="n">Response</span> <span class="n">response</span> <span class="o">=</span> <span class="n">mOkHttpClient</span><span class="o">.</span><span class="na">newCall</span><span class="o">(</span><span class="n">loginRequest</span><span class="o">).</span><span class="na">execute</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="k">try</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="kt">byte</span><span class="o">[]</span> <span class="n">bytes</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="o">[</span><span class="mi">1024</span><span class="o">];</span>
</span></span><span class="line"><span class="cl">            <span class="n">InputStream</span> <span class="n">is</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="na">body</span><span class="o">().</span><span class="na">byteStream</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">            <span class="k">while</span> <span class="o">(</span><span class="n">is</span><span class="o">.</span><span class="na">read</span><span class="o">(</span><span class="n">bytes</span><span class="o">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">                <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="n">bytes</span><span class="o">,</span> <span class="s">&#34;GBK&#34;</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2018-07-16</span>
            </div></div>
        <div class="post-info-line">
            <div class="post-info-md"><span>
                            <a class="link-to-markdown" href="/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/index.md" target="_blank">阅读原始文档</a>
                        </span></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="http://example.org/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/" data-title="使用HttpClient和OkHttp实现模拟登录方正教务系统" data-hashtags="爬虫"><i class="fab fa-twitter fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="http://example.org/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/" data-hashtag="爬虫"><i class="fab fa-facebook-square fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Hacker News" data-sharer="hackernews" data-url="http://example.org/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/" data-title="使用HttpClient和OkHttp实现模拟登录方正教务系统"><i class="fab fa-hacker-news fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="http://example.org/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/" data-title="使用HttpClient和OkHttp实现模拟登录方正教务系统"><i data-svg-src="https://cdn.jsdelivr.net/npm/simple-icons@7.3.0/icons/line.svg" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="http://example.org/%E4%BD%BF%E7%94%A8httpclient%E5%92%8Cokhttp%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E7%99%BB%E5%BD%95%E6%96%B9%E6%AD%A3%E6%95%99%E5%8A%A1%E7%B3%BB%E7%BB%9F/" data-title="使用HttpClient和OkHttp实现模拟登录方正教务系统" data-ralateuid="xxxx"><i class="fab fa-weibo fa-fw" aria-hidden="true"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw" aria-hidden="true"></i>&nbsp;<a href="/tags/%E7%88%AC%E8%99%AB/">爬虫</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8B/" class="prev" rel="prev" title="ArrayList源码分析（下）——Java8中新增的Spliterator的分析"><i class="fas fa-angle-left fa-fw" aria-hidden="true"></i>ArrayList源码分析（下）——Java8中新增的Spliterator的分析</a>
            <a href="/threadlocal/" class="next" rel="next" title="ThreadLocal原理解析">ThreadLocal原理解析<i class="fas fa-angle-right fa-fw" aria-hidden="true"></i></a></div>
</div>
<div id="comments"><div id="valine" class="comment"></div><noscript>
                Please enable JavaScript to view the comments powered by <a href="https://valine.js.org/">Valine</a>.
            </noscript></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.109.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.11"><i class="far fa-kiss-wink-heart fa-fw" aria-hidden="true"></i> LoveIt</a>
                </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw" aria-hidden="true"></i><span itemprop="copyrightYear">2023</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">MC</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw" aria-hidden="true"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw" aria-hidden="true"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/valine/valine.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/valine@1.5.0/dist/Valine.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.38.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lunr@2.3.9/lunr.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.stemmer.support.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.zh.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.3.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.11/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.5.1/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":50},"comment":{"valine":{"appId":"QGzwQXOqs5JOhN4RGPOkR2mR-MdYXbMMI","appKey":"WBmoGyJtbqUswvfLh6L8iEBr","avatar":"mp","el":"#valine","emojiCDN":"https://cdn.jsdelivr.net/npm/emoji-datasource-google@14.0.0/img/google/64/","emojiMaps":{"100":"1f4af.png","alien":"1f47d.png","anger":"1f4a2.png","angry":"1f620.png","anguished":"1f627.png","astonished":"1f632.png","black_heart":"1f5a4.png","blue_heart":"1f499.png","blush":"1f60a.png","bomb":"1f4a3.png","boom":"1f4a5.png","broken_heart":"1f494.png","brown_heart":"1f90e.png","clown_face":"1f921.png","cold_face":"1f976.png","cold_sweat":"1f630.png","confounded":"1f616.png","confused":"1f615.png","cry":"1f622.png","crying_cat_face":"1f63f.png","cupid":"1f498.png","dash":"1f4a8.png","disappointed":"1f61e.png","disappointed_relieved":"1f625.png","dizzy":"1f4ab.png","dizzy_face":"1f635.png","drooling_face":"1f924.png","exploding_head":"1f92f.png","expressionless":"1f611.png","face_vomiting":"1f92e.png","face_with_cowboy_hat":"1f920.png","face_with_hand_over_mouth":"1f92d.png","face_with_head_bandage":"1f915.png","face_with_monocle":"1f9d0.png","face_with_raised_eyebrow":"1f928.png","face_with_rolling_eyes":"1f644.png","face_with_symbols_on_mouth":"1f92c.png","face_with_thermometer":"1f912.png","fearful":"1f628.png","flushed":"1f633.png","frowning":"1f626.png","ghost":"1f47b.png","gift_heart":"1f49d.png","green_heart":"1f49a.png","grimacing":"1f62c.png","grin":"1f601.png","grinning":"1f600.png","hankey":"1f4a9.png","hear_no_evil":"1f649.png","heart":"2764-fe0f.png","heart_decoration":"1f49f.png","heart_eyes":"1f60d.png","heart_eyes_cat":"1f63b.png","heartbeat":"1f493.png","heartpulse":"1f497.png","heavy_heart_exclamation_mark_ornament":"2763-fe0f.png","hole":"1f573-fe0f.png","hot_face":"1f975.png","hugging_face":"1f917.png","hushed":"1f62f.png","imp":"1f47f.png","innocent":"1f607.png","japanese_goblin":"1f47a.png","japanese_ogre":"1f479.png","joy":"1f602.png","joy_cat":"1f639.png","kiss":"1f48b.png","kissing":"1f617.png","kissing_cat":"1f63d.png","kissing_closed_eyes":"1f61a.png","kissing_heart":"1f618.png","kissing_smiling_eyes":"1f619.png","laughing":"1f606.png","left_speech_bubble":"1f5e8-fe0f.png","love_letter":"1f48c.png","lying_face":"1f925.png","mask":"1f637.png","money_mouth_face":"1f911.png","nauseated_face":"1f922.png","nerd_face":"1f913.png","neutral_face":"1f610.png","no_mouth":"1f636.png","open_mouth":"1f62e.png","orange_heart":"1f9e1.png","partying_face":"1f973.png","pensive":"1f614.png","persevere":"1f623.png","pleading_face":"1f97a.png","pouting_cat":"1f63e.png","purple_heart":"1f49c.png","rage":"1f621.png","relaxed":"263a-fe0f.png","relieved":"1f60c.png","revolving_hearts":"1f49e.png","right_anger_bubble":"1f5ef-fe0f.png","robot_face":"1f916.png","rolling_on_the_floor_laughing":"1f923.png","scream":"1f631.png","scream_cat":"1f640.png","see_no_evil":"1f648.png","shushing_face":"1f92b.png","skull":"1f480.png","skull_and_crossbones":"2620-fe0f.png","sleeping":"1f634.png","sleepy":"1f62a.png","slightly_frowning_face":"1f641.png","slightly_smiling_face":"1f642.png","smile":"1f604.png","smile_cat":"1f638.png","smiley":"1f603.png","smiley_cat":"1f63a.png","smiling_face_with_3_hearts":"1f970.png","smiling_imp":"1f608.png","smirk":"1f60f.png","smirk_cat":"1f63c.png","sneezing_face":"1f927.png","sob":"1f62d.png","space_invader":"1f47e.png","sparkling_heart":"1f496.png","speak_no_evil":"1f64a.png","speech_balloon":"1f4ac.png","star-struck":"1f929.png","stuck_out_tongue":"1f61b.png","stuck_out_tongue_closed_eyes":"1f61d.png","stuck_out_tongue_winking_eye":"1f61c.png","sunglasses":"1f60e.png","sweat":"1f613.png","sweat_drops":"1f4a6.png","sweat_smile":"1f605.png","thinking_face":"1f914.png","thought_balloon":"1f4ad.png","tired_face":"1f62b.png","triumph":"1f624.png","two_hearts":"1f495.png","unamused":"1f612.png","upside_down_face":"1f643.png","weary":"1f629.png","white_frowning_face":"2639-fe0f.png","white_heart":"1f90d.png","wink":"1f609.png","woozy_face":"1f974.png","worried":"1f61f.png","yawning_face":"1f971.png","yellow_heart":"1f49b.png","yum":"1f60b.png","zany_face":"1f92a.png","zipper_mouth_face":"1f910.png","zzz":"1f4a4.png"},"enableQQ":false,"highlight":true,"lang":"zh-CN","pageSize":10,"placeholder":"你的评论 ...","recordIP":true,"serverURLs":"https://leancloud.hugoloveit.com","visitor":true}},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
